package cn.zn.framework.common.utils;

import cn.hutool.core.util.StrUtil;
import cn.zn.framework.common.xss.SQLFilter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Map;

/**
 * 查询参数
 */
public class Query<T> {

    private Map<String, Object> params;

    public Query() {
    }

    public Query(Map<String, Object> params) {
        this.params = params;
    }

    public IPage<T> getPage() {
        return this.getPage(null, false);
    }

    public IPage<T> getPage(String defaultOrderField, boolean isAsc) {
        //分页参数
        long curPage = 1;
        long limit = 10;

        if (this.params.get(Constant.LIMIT) != null) {
            limit = Long.parseLong(this.params.get(Constant.LIMIT).toString());
        }
        if (this.params.get(Constant.OFFSET) != null && Long.parseLong(this.params.get(Constant.OFFSET).toString()) > 0) {
            curPage = Long.parseLong(this.params.get(Constant.OFFSET).toString()) / limit + 1;
        }

        //分页对象
        Page<T> page = new Page<>(curPage, limit);

        //分页参数
        this.params.put(Constant.OFFSET, page);

        //排序字段
        String sort = StrUtil.toUnderlineCase((String) this.params.get(Constant.SORT));
        String order = (String) this.params.get(Constant.ORDER);
        //防止SQL注入（因为sort、order是通过拼接SQL实现排序的，会有SQL注入风险）
        sort = SQLFilter.sqlInject(sort);

        //前端字段排序
        if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
            if (Constant.ASC.equalsIgnoreCase(order)) {
                return page.addOrder(OrderItem.asc(sort));
            } else {
                return page.addOrder(OrderItem.desc(sort));
            }
        }

        //默认排序
        if (isAsc) {
            page.addOrder(OrderItem.asc(defaultOrderField));
        } else {
            page.addOrder(OrderItem.desc(defaultOrderField));
        }

        return page;
    }

    @Deprecated
    public IPage<T> getPage(Map<String, Object> params) {
        return this.getPage(params, null, false);
    }

    @Deprecated
    public IPage<T> getPage(Map<String, Object> params, String defaultOrderField, boolean isAsc) {
        //分页参数
        long curPage = 1;
        long limit = 10;

        if (params.get(Constant.LIMIT) != null) {
            limit = Long.parseLong(params.get(Constant.LIMIT).toString());
        }
        if (params.get(Constant.OFFSET) != null && Long.parseLong(params.get(Constant.OFFSET).toString()) > 0) {
            curPage = Long.parseLong(params.get(Constant.OFFSET).toString()) / limit + 1;
        }

        //分页对象
        Page<T> page = new Page<>(curPage, limit);

        //分页参数
        params.put(Constant.OFFSET, page);

        //排序字段
        String sort = StrUtil.toUnderlineCase((String) params.get(Constant.SORT));
        String order = (String) params.get(Constant.ORDER);
        //防止SQL注入（因为sort、order是通过拼接SQL实现排序的，会有SQL注入风险）
        sort = SQLFilter.sqlInject(sort);

        //前端字段排序
        if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
            if (Constant.ASC.equalsIgnoreCase(order)) {
                return page.addOrder(OrderItem.asc(sort));
            } else {
                return page.addOrder(OrderItem.desc(sort));
            }
        }

        //默认排序
        if (isAsc) {
            page.addOrder(OrderItem.asc(defaultOrderField));
        } else {
            page.addOrder(OrderItem.desc(defaultOrderField));
        }

        return page;
    }
}
